<template>
    <el-dialog v-model="visible" title="导入" width="30%">
        <el-form label-width="120px">
            <el-form-item label="分类文件">
                <el-upload class="upload-demo" action="/api/admin/product/category/importData"
                    :on-success="onUploadSuccess" :headers="headers">
                    <el-button type="primary">上传</el-button>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="visible = false">提交</el-button>
                <el-button @click="visible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>

</template>

<script setup lang='ts'>
import { ref } from 'vue'
import { getToken } from '@/utils/auth'
const visible = ref(false)
const headers = {
    'X-Token': getToken()     // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
}

const onUploadSuccess = (res: any) => {
    if (res.code === 200) {
        ElMessage.success('导入成功');
        visible.value = false;
        // fetchData();
    } else {
        ElMessage.error(res.message || '导入失败');
    }
}

const show = () => {
    visible.value = true;
}
defineExpose({
    show
})

</script>

<style></style>